package com.system.userconsumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.system.userconsumer.domain.Address;
import com.system.userconsumer.domain.User;
import com.system.userconsumer.feign.AddressFeign;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @author Lisiyu
 * date 2022/10/19 20:03
 * version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private AddressFeign addressFeign;

    @GetMapping("/info/{id}")
    public User findById(@PathVariable Integer id){
        User user = new User();
        user.setId(id);
        user.setName("lsy");
        //需要地址信息，找到地址的程序获取
        Address address = addressFeign.findAddressById(id);
        user.setAddress(address);
        return user;
    }

    @GetMapping("/info2/{id}")
    public User findById2(Integer id){
        User user = new User();
        user.setId(id);
        user.setName("woaini");
        Address aaa = addressFeign.findByNameAndId(100, "aaa");
        user.setAddress(aaa);
        return user;
    }

    @GetMapping("/test1")
    @HystrixCommand(fallbackMethod = "test1jiangji")
    public String test1() {
        System.out.println(Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "test1" + System.currentTimeMillis();
    }

    @GetMapping("/test2")
    public String test2(){
        return "test2v2-ffff---" + System.currentTimeMillis();
    }

    @GetMapping("/test3")
    @HystrixCommand(commandKey = "lisiyu",fallbackMethod = "test3fallback",commandProperties ={@HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE")} )
    public String test3(){
        System.out.println(Thread.currentThread().getName());
        if (new Random().nextBoolean()){
            int i = 1/0;
        }
        return "test3" + System.currentTimeMillis();
    }
    public String test1jiangji() {
        System.out.println(Thread.currentThread().getName());
        return "zheshitest1dejiangji";
    }
    public String test3fallback() {
        System.out.println(Thread.currentThread().getName());
        return "大学的恋情基本都不会有结局,所以早分晚分都得分";
    }

}
